package com.lzj.weather02;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

public class WeatherGroupingComparator extends WritableComparator {
    /**
     * 必须写构造器
     */
    public WeatherGroupingComparator() {
        super(Weather.class, true);
    }

    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        //将对象转型
        Weather w1 = (Weather) a;
        Weather w2 = (Weather) b;
        //开始比较对象w1和w2(adcode-->year-->month)
        int result = w1.getAdcode().compareTo(w2.getAdcode());
        if (result == 0) {
            result = w1.getYear().compareTo(w2.getYear());
            if (result == 0) {
                result = w1.getMonth().compareTo(w2.getMonth());
            }
        }
        return result;
    }
}